<template>
	<view class="bodyContent">
		<!-- 登录 -->
		<view class="login_er">
			<!-- 头部 -->
			<view class="loginHeader">
				<view>绑定手机号</view>
			</view>
			<view class="mainContent">
				<!-- 验证码提示 -->
				<view class="authCode">
					<view>
						<input type="text"  v-model="phone" placeholder="手机号" @blur="registerBlur">
					</view>
					<view>
						<input type="text" v-model="code" placeholder="验证码">
						<button :disabled="isSend1" @click="phoneCode">{{isSend1?count1 + 's':codeMsg1}}</button>
					</view>
				</view>
				<!-- 登录 -->
				<view class="toLogin" @click="registerSubmit">{{btnMsg}}</view>
			</view>
		</view>
		<u-modal v-model="showModal" :show-cancel-button="showCancelButton" :confirm-text="confirmText" :cancel-text="cancelText" @confirm="Modalconfirm" :content="showContent"></u-modal>
	</view>
</template>
<script>
	export default{
		data (){
			return {
				codeMsg:'获取验证码',
				phone:'',
				code:'',
				showModal:false,
				showCancelButton:true,
				confirmText:'注册',
				cancelText:'取消',
				showContent:'',
				btnMsg:'绑定登录',
				isSend1:false,
				codeMsg1:'获取验证码',
				count1:0
			}
		},
		methods:{
			onLoad(options){
				this.info = options.info
			},
			//手机验证
			registerBlur() {
				//手机正则表达式
				let phoneCode = /^1[3456789]\d{9}$/
			    if (!(phoneCode.test(this.phone))) {
			        uni.showToast({title:'请输入正确的手机号码',icon:'none'})
			        return;
			    }
			    this.$api.authorizeVerifyMobile({phone:this.phone}).then((res)=>{
			        this.tips = res.data.message
			        if(res.data.returnCode == '20011'){
						uni.showToast({title:'该帐号还未注册',icon:'none'})
			        }else{
			            this.btnMsg = '绑定登录'
						uni.showToast({title:'该帐号已注册，请验证绑定。',icon:'none'})
			        }
			    })
			},
			// 验证码
			phoneCode() {
			    let _this = this;
				//手机正则表达式
				let phoneCode = /^1[3456789]\d{9}$/
			    if (_this.phone != '' && (phoneCode.test(this.phone))) {
			        this.$api.sedNote(_this.phone,'SignIn').then(function (res) {
			            if (res.data.returnCode == 200) {
							uni.showToast({title:res.data.message,icon:'none'})
			                if (!_this.isSend1) {
			                    const TIME_COUNT = 60;
			                    if (!_this.timer1) {
			                        _this.count1 = TIME_COUNT;
			                        _this.isSend1 = true;
			                        _this.timer1 = setInterval(() => {
			                            if (_this.count1 > 0 && _this.count1 <= TIME_COUNT) {
			                                _this.count1--;
			                            } else {
			                                _this.isSend1 = false;
			                                _this.codeMsg1 = '获取验证码'
			                                clearInterval(_this.timer1);
			                                _this.timer1 = null;
			                            }
			                        }, 1000)
			                    }
			                }
			            }
			            else {
							uni.showToast({title:res.data.message,icon:'none'})
			            }
			        })
					.catch(function (error) {
						console.log(error)
					})
				}
			    else {
					uni.showToast({title:'请输入正确的手机号码',icon:'none'})
			    }
			},
			registerSubmit() {
			    let _this = this
				let phoneCode = /^1[3456789]\d{9}$/
				//密码正则表达式
				let passCode = /(?!.*\s)(?!^[\u4E00-\u9FA5]+$)(?!^[a-zA-Z]+$)(?!^[\d]+$)(?!^[^\u4E00-\u9FA5a-zA-Z\d]+$)^.{6,20}$/
			    if (!(phoneCode.test(_this.phone))) {
					uni.showToast({title:'请输入正确的手机号码',icon:'none'})
			    }
			    else if (_this.code == '') {
					uni.showToast({title:'验证码不能为空',icon:'none'})
			    }else{
					const data = JSON.parse(this.info)
					data.code = _this.code
					data.phone = _this.phone
					this.$api.authorizebindPhone(data).then((res)=>{
						if (res.data.returnCode == 200) {
							localStorage.setItem('token', res.data.result.token);
							uni.redirectTo({
								url: '/pages/index/index'
							});
						}else {
							uni.showToast({title:res.data.message,icon:'none'})
						}
					})
			    }
			},
		}
	}
</script>
<style lang="scss" scoped>
	@import  '../../static/css/login/login.scss'
</style>
